enum VARS {
    tm1,
    tm2,
    tm3,
    tm4
}

enum CLOCKWISEE {
    //% block="正转"
    True,
    //% block="反转"
    False
}


//% color="#2494F4" iconWidth=50 iconHeight=40
namespace MicroblueTtlMotor{
    
    //% block="串口电机[V]，初始化: TX=[TX], RX=[RX]" blockType="command"
    //% V.shadow="dropdownRound"   V.options="VARS"     V.defl="VARS.tm1"
    //% RX.shadow="dropdownRound" RX.options="PINS" RX.defl=PINS.P0
    //% TX.shadow="dropdownRound" TX.options="PINS" TX.defl="1"

    export function motor_init(parameter: any, block: any) {
        let v = parameter.V.code; 
        let rx = parameter.RX.code; 
        let tx = parameter.TX.code;
        Generator.addImport("from mb_ttl_motor import TtlMotor", true);
        Generator.addCode(`${v} = TtlMotor(rx=${rx}, tx=${tx})`);
    }

  
    //% block="[V]电机模式: 速度=[SPEED], 方向=[DIR]" blockType="command"
    //% V.shadow="dropdownRound"   V.options="VARS"     V.defl="VARS.tm1"
    //% SPEED.shadow="number" SPEED.min=0 SPEED.max=1000  SPEED.defl=1000
    //% DIR.shadow="dropdownRound" DIR.options="CLOCKWISEE" DIR.defl="CLOCKWISEE.True"

    export function motor_run(parameter: any, block: any) {
        let v = parameter.V.code; 
        let speed=parameter.SPEED.code;
        let direction=parameter.DIR.code;
        Generator.addCode(`${v}.motor_run(${speed}, ${direction})`);
    }
    

    //% block="[V]电机模式: 运行时间=[SECONDS], 方向=[DIR]" blockType="command"
    //% V.shadow="dropdownRound"   V.options="VARS"     V.defl="VARS.tm1"
    //% SECONDS.shadow="number" SECONDS.min=0 SECONDS.max=360  SECONDS.defl=10
    //% DIR.shadow="dropdownRound" DIR.options="CLOCKWISEE" DIR.defl="CLOCKWISEE.True"

    export function motor_run_seconds(parameter: any, block: any) {
        let v = parameter.V.code; 
        let seconds=parameter.SECONDS.code;
        let direction=parameter.DIR.code;
        Generator.addCode(`${v}.motor_run_seconds(${seconds}, ${direction})`);
    }


    //% block="[V]舵机模式: 置零" blockType="command"
    //% V.shadow="dropdownRound"   V.options="VARS"     V.defl="VARS.tm1"

    export function ttl_motor_reset(parameter: any, block: any) {
        let v = parameter.V.code; 
        Generator.addCode(`${v}.motor_reset()`);
    }


    //% block="[V]舵机模式: 相对角度=[ANGLE], 方向=[DIR]" blockType="command"
    //% V.shadow="dropdownRound"   V.options="VARS"     V.defl="VARS.tm1"
    //% ANGLE.shadow="number" ANGLE.defl=180
    //% DIR.shadow="dropdownRound" DIR.options="CLOCKWISEE" DIR.defl="CLOCKWISEE.True"

    export function servo_relative_angle(parameter: any, block: any) {
        let v = parameter.V.code; 
        let angle=parameter.ANGLE.code;
        let direction=parameter.DIR.code;
        if(direction == 'False') {
            Generator.addCode(`${v}.servo_relative_angle(-${angle})`);
        } else {
            Generator.addCode(`${v}.servo_relative_angle(${angle})`);
        }
    }
    

    //% block="[V]舵机模式: 绝对角度=[ANGLE]" blockType="command"
    //% V.shadow="dropdownRound"   V.options="VARS"     V.defl="VARS.tm1"
    //% ANGLE.shadow="number"  ANGLE.defl=180

    export function servo_absolute_angle(parameter: any, block: any) {
        let v = parameter.V.code; 
        let angle=parameter.ANGLE.code;
        Generator.addCode(`${v}.servo_absolute_angle(${angle})`);
    }


    //% block="[V]舵机模式: 相对圈=[CIRCLES], 方向=[DIR]" blockType="command"
    //% V.shadow="dropdownRound"   V.options="VARS"     V.defl="VARS.tm1"
    //% CIRCLES.shadow="number"  CIRCLES.defl=10
    //% DIR.shadow="dropdownRound" DIR.options="CLOCKWISEE" DIR.defl="CLOCKWISEE.True"

    export function servo_relative_circles(parameter: any, block: any) {
        let v = parameter.V.code; 
        let circles=parameter.CIRCLES.code;
        let direction=parameter.DIR.code;
        if(direction == 'False') {
            Generator.addCode(`${v}.servo_relative_circles(-${circles})`);
        } else {
            Generator.addCode(`${v}.servo_relative_circles(${circles})`);
        }
    }


    //% block="[V]电机模式: 读取速度" blockType="reporter"
    //% V.shadow="dropdownRound"   V.options="VARS"     V.defl="VARS.tm1"

    export function motor_get_speed(parameter: any, block: any) {
        let v = parameter.V.code; 
        Generator.addCode(`${v}.motor_get_speed()`);
    }


    //% block="[V]舵机模式: 读取相对角度" blockType="reporter"
    //% V.shadow="dropdownRound"   V.options="VARS"     V.defl="VARS.tm1"

    export function servo_get_relative_angle(parameter: any, block: any) {
        let v = parameter.V.code; 
        Generator.addCode(`${v}.get_relative_angle()`);
    }


    //% block="[V]舵机模式: 读取绝对角度" blockType="reporter"
    //% V.shadow="dropdownRound"   V.options="VARS"     V.defl="VARS.tm1"

    export function servo_get_absolute_angle(parameter: any, block: any) {
        let v = parameter.V.code; 
        Generator.addCode(`${v}.get_absolute_angle()`);
    }

    //% block="[V]舵机模式: 转动圈数" blockType="reporter"
    //% V.shadow="dropdownRound"   V.options="VARS"     V.defl="VARS.tm1"

    export function servo_get_circles(parameter: any, block: any) {
        let v = parameter.V.code; 
        Generator.addCode(`${v}.get_circles()`);
    }
}
